﻿<?php

/**
 * ajax actions.
 *
 * @package    shop
 * @subpackage ajax
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ajaxActions extends sfActionsEx {

    public function executeUnregisterNewsletter(sfRequest $request) {
        $json = new stdClass();
        if ($request->getParameter('newsletter_email')) {
            $newsletter = NewsletterTable::getInstance()->findOneBy('email', $request->getParameter('newsletter_email'))->getFirst();
            if ($newsletter && $newsletter->getActive()) {
                $newsletter->setActive(false);
                $newsletter->save();
                $json->message = 'Podany email został usunięty z bazy newslettera.';
                $json->status = 1;
            } else {
                $json->message = 'Nie znaleziono podanego maila w bazie newslettera.';
                $json->status = 0;
            }
        } else {
            $json->message = 'Nie podano zadnego maila';
            $json->status = 0;
        }
        echo json_encode($json);
        die;
    }

    public function executeRegisterNewsletter(sfRequest $request) {
        $json = new stdClass();
        if ($request->getParameter('newsletter_email')) {
            $newsletter = NewsletterTable::getInstance()->findOneBy('email', $request->getParameter('newsletter_email'));
            if (!$newsletter) {
                $newsletter = new Newsletter();
                $newsletter->setEmail($request->getParameter('newsletter_email'));
            }
            if ($newsletter) {
                if ($newsletter->getActive()) {
                    $json->status = 0;
                    $json->message = 'Jesteś już zapisany w newsletterze';
                } else {
                    $json->status = 1;
                    $json->message = 'Zostałe dopisany do newslettera';
                    $newsletter->setActive(true);
                    $newsletter->save();
                }
            }
            echo json_encode($json);
            die;
        }
        return sfView::NONE;
    }

    public function executeLogin(sfRequest $request) {
        $post = $this->prepareRequest($request, array('login', 'password'));
        $json = new stdClass();
        $json->status = 0;
        $json->messageType = 'warning';
        if ($post->status === 1) {
            $json->message = 'Nie wypełniono wszystkich pól';
        } else if ($post->status === 2) {
            if (Authorization::getInstance()->create($post->values["login"], $post->values["password"])) {
                $json->status = 1;
                $json->mesageType = 'success';
                $json->message = 'Zalogowano poprawnie.';
            } else {
                $json->message = 'Nie znaleziono podanego użytkownika.';
            }
        }
        echo json_encode($json);
        die;
    }

    public function executeRegisterAccount(sfRequest $request) {
        $post = $this->prepareRequest($request, array("account_name", "password", "re_password", "email", "name", "surname", "adress", "telephone", "birthday"));
        $json = new stdClass();
        $json->status = 0;
        $json->messageType = 'warning';
	$json->post = $post;
        if ($post->status === 1) {
            $json->message = 'Nie wypełniono wszystkich pól';
            $json->post = $post->value;
        } else if ($post->status === 2) {
            if (UserTable::getInstance()->findOneBy('name', $post->values['account_name'])) {
                $json->message = 'Podana nazwa użytkownika istnieje w naszej bazie danych';
            } else {
                if ($post->values['password'] == $post->values['re_password']) {
                    $json->message = 'Konto zostało utworzone poprawnie';
 		    $json->mesageType = 'success';
                    $json->status = 1;
                    $account = new User();
                    $account->setName($post->values['account_name'])
                            ->setPassword($post->values['password'])
                            ->setEmail($post->values['email'])
                            ->save();
                    $data = new DeliveryData();
                    $data->setName($post->values['name'])
                            ->setSurname($post->values['surname'])
                            ->setAdress($post->values['adress'])
                            ->setTelephone($post->values['telephone'])
                            ->setBirthday($post->values['birthday'])
                            ->setUserId($account->getId())
                            ->save();
                    Authorization::getInstance()->create($account->getName(), $account->getPassword());
                }
            }
        }
        echo json_encode($json);
        die;
    }

    public function executeLogout(sfRequest $request) {
        $json = new stdClass();
        $json->status = 0;
        $json->message = 'Nie mozna wylogowac';
        $json->messageType = 'warning';
        if (Authorization::getInstance()->isExists()) {
            Authorization::getInstance()->delete();
            $json->status = 1;
            $json->message = 'Wylogowano';
            $json->messageType = 'success';
        }
        echo json_encode($json);
        die;
    }

    public function executeContact(sfRequest $request) {
        $json = new stdClass();
        $json->status = 0;
        $json->message = 'Wiadomość nie została wysłana poprawnie. Wypełnij wszystkie pola.';
        $json->messageType = 'warning';
        $post = $this->prepareRequest($request, array("contactName", "contactEmail", "contactText"));
        if ($post->status == 2) {
            $json->status = 1;
            $json->message = 'Wiadomość wysłana poprawnie.';
            $json->messageType = 'success';
            $mail = new Mail();
            $mail->setSubject('Contact');
            $mail->addToVarHolder('text', $post->values['contactText']);
            $mail->addToVarHolder('name', $post->values['contactName']);
            $mail->addToVarHolder('email', $post->values['contactEmail']);
            $mail->setTo(sfConfig::get('contact_email'));
            //$mail->send();
        }
        echo json_encode($json);
        die;
    }

    public function executeChangePassword(sfRequest $request) {
        $json = new stdClass();
        $json->status = 0;
        $json->messageType = 'warning';
        $json->message = 'Hasło nie zostało zmienione. Wypełnij wszystkie pola.';
        $post = $this->prepareRequest($request, array("old_password", "re_password", "password"));
        if ($post->status == 2) {
            if (Authorization::getInstance()->isExists()) {
                $user = Authorization::getInstance()->getUser();
                if ($user->getPassword() == $post->values['old_password']) {
                    if ($post->values['password'] == $post->values['re_password']) {
                        $json->message = 'Haslo zostalo zmienione';
                        $json->messageType = 'success';
                        $json->status = 1;
                        $user->setPassword($post->values['password']);
                        $user->save();
                    } else {
                        $json->message = 'Podane hasła są różne';
                    }
                } else {
                    $json->message = 'Podane hasło jest nieprawidłowe';
                }
            } else {
                $json->message = 'Hasło może zmienić tylko zalogowany użytkownik';
            }
        }
        echo json_encode($json);
        die;
    }

    public function executeChangeEmail(sfRequest $request) {
        $json = new stdClass();
        $json->status = 0;        $json->messageType = 'warning';
        $json->message = 'Email nie zostały zmienione. Wypełnij pole.';
        $post = $this->prepareRequest($request, array("email"));
        if ($post->status == 2) {
            if (Authorization::getInstance()->isExists()) {
                $user = Authorization::getInstance()->getUser();
                if ($user->getEmail() != $post->values['email']) {

                    $json->message = 'Email zostal zmieniony.';
                    $json->messageType = 'success';
                    $json->status = 1;
                    $user->setEmail($post->values['email']);
                    $user->save();
                } else {
                    $json->message = 'Podane email jest taki sam jak aktualny';
                }
            } else {
                $json->message = 'Email może zmienić tylko zalogowany użytkownik';
            }
        }
        echo json_encode($json);
        die;
    }
    
    

}
